home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1417 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.2 KB

  1. Path: gate.net!pslfl2-21
  2. From: bhutto@gate.net (William Hutto)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Help a fellow C programer
  5. Date: 13 Jan 1996 10:19:50 GMT
  6. Organization: CyberGate, Inc.
  7. Message-ID: <4d8106$1s4g@news.gate.net>
  8. References: <4d3t4q$ea6@klein.delphi.com> <4d71td$88a@info.uah.edu>
  9. NNTP-Posting-Host: pslfl2-21.gate.net
  10. X-Newsreader: News Xpress Version 1.0 Beta #4
  11.  
  12. In article <4d71td$88a@info.uah.edu>, gbacon@oreo (Greg Bacon) wrote:
  13. >Diane Barker (Barker@mci.newscorp.com) wrote:
  14. >: Can any one tell me the C function/statment that is equal to the math 
  15. >: function ! (factorial).
  16. >
  17. >: Email me at: BARKER@MCI.NEWSCORP.COM  if you can help
  18. >
  19. >: Mike Barker
  20. >
  21. >int factorial(unsigned int a)
  22. >{
  23. >   if ((a == 0) || (a == 1))
  24. >      return 1;
  25. >   else
  26. >      return a * factorial(a-1);
  27. >}
  28. >
  29. >That's a recursive version.  a is unsigned since the factorial operation
  30. >is only defined over [0, \infinity)
  31. >
  32. >Greg
  33.  
  34. This is a non-recursive more robust version.
  35.  
  36. unsigned long factorial(unsigned a)
  37. {
  38. unsigned i=1;
  39. unsigned long retval=1;
  40.     
  41.     if(a>12)        /*overflow*/
  42.         return 0L;
  43.     do {
  44.         retval *= i++;
  45.     } while(i<=a);
  46.     return retval;
  47. }
  48.  
  49. Bill        
  50.  
  51. "Whatcha got on?...Your mind?"
  52.